
proc sql noprint;
 select count(*)
 into :nb_obs
 from dictionary.tables
 where libname = "SDTM"
 and memname = "PE";
quit;

** Get the data and add the SUPP if available;
%get_supp(datain=pe);

proc sort data=pe_supp out=pe;
 by usubjid visitnum peseq;
run;



data adpe;
length avisit $20.;
format ADT yymmdd10. ATM time5. ADTM DATETIME13.;
 merge pe(in=a) 
       adam.adsl(in=b);
 by usubjid;
 if a and b;

 
  *AVISITN;
  if visitnum ne . then AVISITN=int(visitnum);
  else put "%upcase(%str(w)arning: missing visitnum for) " usubjid pedtc= petestcd=;

  *AVISIT;
   avisit=put(avisitn, visitn.);
 if avisit="Early Term" then avisit="Early Term";
 else avisit=avisit;
*VISITYP;

/*if visitnum^=int(visitnum) then visityp="Unscheduled";
  else visityp="Scheduled";*/

  if find(upcase(avisit),'UNSCH')>0 then visityp="Unscheduled";
  else if ^missing(avisit) then visityp="Scheduled";
  else visityp='';

 
 ****ADT ATM ADTM****;

if ^missing(pedtc) then do;
    ADT=input(substr(pedtc,1,10),yymmdd10.);
    if length(pedtc)>10 then do; 
	ATM=input(substr(pedtc,12,5),time5.); 
	ADTM=input(pedtc, E8601DT.); 
	end;
end;

***PARAM PARAMCD***;

 if ^missing(petest)   then PARAM   = strip(petest);
 if ^missing(petestcd) then PARAMCD = strip(petestcd);

 ***PARCAT1 PARCAT1N***;

 if pecat ne " " then PARCAT1 = strip(pecat);
 if parcat1="COMPLETE" then parcat1n=100;
 else parcat1n=200;

  *****SRCDOM****;

  SRCDOM="PE";

  ***SRCSEQ***;
  SRCSEQ=peseq;

  ****AVALC****;

 AVALC  = strip(pestresc);

 ****ADY****;

 ADY = pedy;

 ****ABLFL****;

/* ABLFL =PEBLFL;*/

/*TRTP APERIOD and TRTA */
if adtm ne . then do;

 IF ADTM >= TR01SDTM THEN DO; TRTP=TRT01P; TRTPN=TRT01PN;  TRTA=TRT01A; TRTAN=TRT01AN;* APERIOD=TRT01AN; END;
IF ADTM >= TR02SDTM THEN DO; TRTP=TRT02P; TRTPN=TRT02PN;  TRTA=TRT02A; TRTAN=TRT02AN; *APERIOD=TRT02AN; END;
IF ADTM >= TR03SDTM THEN DO; TRTP=TRT03P; TRTPN=TRT03PN;  TRTA=TRT03A; TRTAN=TRT03AN; *APERIOD=TRT03AN; END;
IF ADTM >= TR04SDTM THEN DO; TRTP=TRT04P; TRTPN=TRT04PN;  TRTA=TRT04A; TRTAN=TRT04AN; *APERIOD=TRT04AN; END;
IF ADTM >= TR05SDTM THEN DO; TRTP=TRT05P; TRTPN=TRT05PN;  TRTA=TRT05A; TRTAN=TRT05AN; *APERIOD=TRT05AN; END;
IF ADTM >= TR06SDTM THEN DO; TRTP=TRT06P; TRTPN=TRT06PN;  TRTA=TRT06A; TRTAN=TRT06AN;* APERIOD=TRT06AN; END;
end;
else if adtm eq . and adt ne . then do;

IF ADT >= TR01SDT THEN DO; TRTP=TRT01P; TRTPN=TRT01PN;  TRTA=TRT01A; TRTAN=TRT01AN; *APERIOD=TRT01AN; END;
IF ADT >= TR02SDT THEN DO; TRTP=TRT02P; TRTPN=TRT02PN;  TRTA=TRT02A; TRTAN=TRT02AN; *APERIOD=TRT02AN; END;
IF ADT >= TR03SDT THEN DO; TRTP=TRT03P; TRTPN=TRT03PN;  TRTA=TRT03A; TRTAN=TRT03AN; *APERIOD=TRT03AN; END;
IF ADT >= TR04SDT THEN DO; TRTP=TRT04P; TRTPN=TRT04PN;  TRTA=TRT04A; TRTAN=TRT04AN; *APERIOD=TRT04AN; END;
IF ADT >= TR05SDT THEN DO; TRTP=TRT05P; TRTPN=TRT05PN;  TRTA=TRT05A; TRTAN=TRT05AN; *APERIOD=TRT05AN; END;
IF ADT >= TR06SDT THEN DO; TRTP=TRT06P; TRTPN=TRT06PN;  TRTA=TRT06A; TRTAN=TRT06AN;* APERIOD=TRT06AN; END;
end;

 IF avisitn in (-2 -1 999) then do; TRTP=" "; TRTPN=.; TRTA=""; TRTAN=.;/*APERIOD=.;*/ end;
 *else if avisitn not in (-2,-1,999) then  aperiod=input(strip(scan(avisit, 2, '')), best.);
*if aperiod ne . then aperiodc="Period 0"||strip(put(aperiod,best.));
*else aperiodc=" ";


 *PARAMN;

 select(paramcd);
   when("APPEAR")  paramn = 1;
   when("CARDIO")   paramn = 2;
   when("GASTRO")    paramn = 3;
   when("HEENT")    paramn = 4;
   when("MUSCULO")    paramn = 5;
   when("NECK")   paramn=6;
   when("NEURO")   paramn=7;
   when("RESP")   paramn=8;
   when("SKIN")   paramn=9;
   otherwise        paramn = 999;
 end;


 format avisit;

 ****PEABNFL;
/**/
/* if index(avalc,"Abnormal")>0 then PEABNFL="Y";*/
/* else PEABNFL="N";*/

/* if avalc="ABNORMAL CS" then peclsig = "YES";*/
/* else if avalc="ABNORMAL NCS" then peclsig = "NO";*/
/* else peclsig="";*/

 
 ***ANL01FL***;

if avalc ne " " then ANL01FL="Y";
/*drop peclsig;*/

run;

***create ANL01FL;
/*
proc sort data=adpe;
   by USUBJID PARAMN PARAMCD avisitn ;
run;

data tblfl
     unsc;
 set adpe;

 if visit eq: "Unsc" then output unsc;
 else                     output tblfl;

run;

proc sort data=unsc;
   by USUBJID PARAMN PARAMCD avisitn;
run;

proc sort data=tblfl;
   by USUBJID PARAMN PARAMCD avisitn;
run;

data tblfl;
   length ANL01FL $1.;
   set tblfl;
   by USUBJID PARAMN PARAMCD avisitn;

   if avisitn in (0,1) then
      do;
         if last.avisitn then ANL01FL = "Y";
      end;

   else if (avisitn > 1) then
      do;
         if first.avisitn then ANL01FL = "Y";
      end;

  label ANL01FL  = "Analysis Record Flag 01";
 
run;

data adpe;
   set tblfl
       unsc;
   by USUBJID PARAMN PARAMCD avisitn;
run;*/
*********attributes**********;

data adpe_;
	set adpe;
 attrib STUDYID     label = "Study Identifier"
        USUBJID     label = "Unique Subject Identifier"
        SUBJID      label = "Subject Identifier for the Study"
        SITEID      label = "Study Site Identifier"
		ARM         label = "Description of Planned Arm"
        AVISITN     label = "Analysis Visit (N)" 
        AVISIT      label = "Analysis Visit"    
	    VISITYP     label = "Type of Visit"     
        ADTM        label = "Analysis Datetime" 
		ADT         label = "Analysis Date"
        ADY         label = "Analysis Relative Day"
		ATM         label = "Analysis Time"
        PARAM       label = "Parameter"          
        PARAMCD     label = "Parameter Code"    
        PARAMN      label = "Parameter (N)"
        AVALC       label = "Analysis Value (C)" 
        TRTP        label = "Planned Treatment" 
		TRTPN       label = "Planned Treatment (N)"
		TRTA        label = "Actual Treatment"  
		TRTAN       label = "Actual Treatment (N)"
/*		APERIOD     label = "Period"*/
/*		APERIODC    label = "Period (C)"        */
	    PARCAT1     label = "Parameter Category 1" 
  		PARCAT1N    label = "Parameter Category 1 (N)"
        SRCDOM      label = "Source Data"
        SRCSEQ      label = "Source Sequence Number"
		/*PEABNFL     label = "Abnormality Flag"*/
/*		ABLFL       label = "Baseline Record Flag"*/
/*		PECLSIG     label = "Clinical Significance"*/
		ANL01FL     label = "Analysis Record Flag 01";
	run;


proc SQL;
   create table adamw.adpe(label = "Physical Examinations" sortedby = STUDYID USUBJID SRCSEQ) as
   SELECT l.STUDYID,l.USUBJID,l.SUBJID,l.SITEID,l.ARMCD,l.ACTARMCD,
          l.ENRLFL,l.SAFFL,l.RANDAFL,l.RANDBFL,l.COMPLFL,l.PKFL,l.PDFL,l.TRTSDT,l.TRTSDTM,l.TRTEDT,l.TRTEDTM,
          r.TRTP,r.TRTPN,r.TRTA,r.TRTAN,/*r.APERIOD,r.APERIODC,*/r.AVISIT,r.AVISITN,r.VISITYP,r.ADT,r.ATM,r.ADTM,r.ADY,
          r.PARCAT1,r.PARCAT1N,r.PARAM,r.PARAMCD,r.PARAMN,
          r.AVALC, r.ANL01FL,/*r.ABLFL,*//*r.PECLSIG,*/r.PETESTOT,r.PESTAT,r.PESPID,r.SRCDOM,r.SRCSEQ
          
       
   FROM adam.adsl(where=(armcd ^in("SCRNFAIL"))) as l, adpe_ (where=(paramcd ^in("PEALL"))) as r
   WHERE l.usubjid = r.usubjid
   order by STUDYID, USUBJID,SRCSEQ,AVISITN,PARAMN
   ;
QUIT;
proc contents data= adamw.adpe;
run;


%clrW;






